﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Model;
using SDT.Site.ViewModels;

namespace SDT.Site.Controllers
{   
    public class InterestController : Controller
    {       
        private IList<Interest> calInterest = new List<Interest>();
        private SDTDataContext db = new SDTDataContext();        
        public static double soTienVay = 0;
        public static double laiPhaiTraBanDau = 0;
        public static int dem = 0;
        public ActionResult Index()
        {            
            return View();
        }
        [HttpPost]
        public ActionResult Index(FormCollection collection)
        {            
            string calc = Request.Params["cal"];
            int kieuvay = int.Parse(Request.Params["ddlMoneyType"]);
            soTienVay = double.Parse(Request.Params["sotienvay"]);
            double LaiXuatVay = double.Parse(Request.Params["laixuatvay"])/100;
            int KieuLaiXuatVay = int.Parse(Request.Params["kieulaixuatvay"]);
            if (KieuLaiXuatVay == 2)
            {
                LaiXuatVay = LaiXuatVay / 12;
            }
            laiPhaiTraBanDau = soTienVay*LaiXuatVay;
            int kieuthoigianvay = int.Parse(Request.Params["cmbtime"]);
            int thoigianvay = int.Parse(Request.Params["thoigianvay"]);
            double TongLaiPhaiTra = 0;
            double TongVonVaLai = 0;
            if (kieuthoigianvay == 2)
            {
                thoigianvay = thoigianvay * 12;
            }
                          
            double SoTienVayThucTe = soTienVay / thoigianvay;
            double LaiPhaiTraThucTe = soTienVay * LaiXuatVay;
            double SoTienVayHienTai = soTienVay;
            if(!string.IsNullOrEmpty(calc))
            {
                //tinh lai xuat chia deu  
                if (kieuvay == 1)
                {
            
                    for (int i = 1; i <= thoigianvay; i++)
                    {
                        var it = new Interest();
                        if (i == 1)
                        {
                            it.SoKyTra = 1;
                            it.DuNoDauKy = soTienVay;
                            it.VonPhaiTra = soTienVay / thoigianvay;
                            it.LaiPhaiTra = laiPhaiTraBanDau;
                            it.VonVaLai = it.VonPhaiTra + it.LaiPhaiTra;
                            calInterest.Add(it);
                        }
                        else
                        {
                            soTienVay = soTienVay - SoTienVayThucTe;
                            it.SoKyTra = i;
                            it.DuNoDauKy = soTienVay;
                            it.VonPhaiTra = SoTienVayThucTe;
                            it.LaiPhaiTra = laiPhaiTraBanDau;
                            it.VonVaLai = it.VonPhaiTra + it.LaiPhaiTra;
                            calInterest.Add(it);
                        }                        
                    }
                    foreach (var item in calInterest)
                    {
                        TongLaiPhaiTra += item.LaiPhaiTra;
                        TongVonVaLai += item.VonVaLai;
                    }
                    ViewData["tonglai"] = TongLaiPhaiTra;
                    ViewData["tongvonvalai"] = TongVonVaLai;
                }                
                //tinh lai xuat giam dan
                if (kieuvay == 2)
                {
                    for (int i = 1; i <= thoigianvay; i++)
                    {
                        var it = new Interest();
                        if (i == 1)
                        {
                            it.SoKyTra = 1;
                            it.DuNoDauKy = soTienVay;
                            it.VonPhaiTra = soTienVay / thoigianvay;
                            it.LaiPhaiTra = laiPhaiTraBanDau;
                            it.VonVaLai = it.VonPhaiTra + it.LaiPhaiTra;
                            calInterest.Add(it);
                        }
                        else
                        {
                            soTienVay = soTienVay - SoTienVayThucTe;
                            laiPhaiTraBanDau = laiPhaiTraBanDau - LaiPhaiTraThucTe / thoigianvay;
                            it.SoKyTra = i;
                            it.DuNoDauKy = soTienVay;
                            it.VonPhaiTra = SoTienVayThucTe;
                            it.LaiPhaiTra = laiPhaiTraBanDau;
                            it.VonVaLai = it.VonPhaiTra + it.LaiPhaiTra;
                            calInterest.Add(it);
                        }
                    }
                    foreach (var item in calInterest)
                    {
                        TongLaiPhaiTra += item.LaiPhaiTra;
                        TongVonVaLai += item.VonVaLai;
                    }
                    ViewData["tonglai"] = TongLaiPhaiTra;
                    ViewData["tongvonvalai"] = TongVonVaLai;
                }
                //tinh lai xuat theo quy
                if (kieuvay == 3)
                {
                    for (int i = 1; i <= thoigianvay; i++)
                    {
                        dem++;
                        var it = new Interest();
                        if (dem == 1 && i > 3)
                        {
                            laiPhaiTraBanDau = laiPhaiTraBanDau - 3 * (LaiPhaiTraThucTe / thoigianvay);
                            soTienVay = soTienVay - 3 * SoTienVayThucTe;
                        }
                        if (dem == 3)
                        {
                            it.SoKyTra = i;
                            it.VonPhaiTra = 3 * (SoTienVayHienTai / thoigianvay);
                            it.DuNoDauKy = soTienVay;
                            it.LaiPhaiTra = laiPhaiTraBanDau;                           
                            it.VonVaLai = it.VonPhaiTra + it.LaiPhaiTra;
                            calInterest.Add(it);
                            dem = 0;
                        }
                        else
                        {
                            it.SoKyTra = i;                         
                            it.DuNoDauKy = soTienVay;
                            it.LaiPhaiTra = laiPhaiTraBanDau;                           
                            it.VonPhaiTra = 0;                            
                            it.VonVaLai = it.VonPhaiTra + it.LaiPhaiTra;
                            calInterest.Add(it);                            
                        }
                    }
                    foreach (var item in calInterest)
                    {
                        TongLaiPhaiTra += item.LaiPhaiTra;
                        TongVonVaLai += item.VonVaLai;
                    }
                    ViewData["tonglai"] = TongLaiPhaiTra;
                    ViewData["tongvonvalai"] = TongVonVaLai;
                }                
            }
            dem = 0;
            laiPhaiTraBanDau = 0;
            soTienVay = 0;
            ViewData["sotienvay"] = double.Parse(Request.Params["sotienvay"]);
            ViewData["thoigianvay"] = int.Parse(Request.Params["thoigianvay"]);
            ViewData["kieuvay"] = int.Parse(Request.Params["ddlMoneyType"]);
            ViewData["kieuthoigianvay"] = int.Parse(Request.Params["cmbtime"]);
            ViewData["laixuatvay"] = double.Parse(Request.Params["laixuatvay"]);
            ViewData["kieulaixuatvay"] = int.Parse(Request.Params["kieulaixuatvay"]);
            var data = calInterest;
            return View(data);
        }
        public static List<SelectListItem> ListKieuVay()
        {
            var data = new List<SelectListItem>();
            var list = new SelectListItem()
            {
               Text = "Tháng",
               Value = "1"
               
            };
            var list1 = new SelectListItem()
            {
                Text = "Năm",
                Value = "2"

            };
            data.Add(list);
            data.Add(list1);
            return data;
            
        }
        public static List<SelectListItem> ListKieuLaiXuat()
        {
            var data = new List<SelectListItem>();
            var list = new SelectListItem()
            {
                Text = "Tháng",
                Value = "1"

            };
            var list1 = new SelectListItem()
            {
                Text = "Năm",
                Value = "2"

            };
            data.Add(list);
            data.Add(list1);
            return data;

        }
        public static List<SelectListItem> ListLoaiHinh()
        {
            var data = new List<SelectListItem>();
            var list = new SelectListItem()
            {
                Text = "Trả lãi chia đều ",
                Value = "1"                
            };
            var list1 = new SelectListItem()
            {
                Text = "Trả lãi giảm dần (Vốn hàng tháng)",
                Value = "2"

            };
            var list2 = new SelectListItem()
            {
                Text = "Trả lãi giảm dần (Vốn hàng quý)",
                Value = "3"

            };
            data.Add(list);
            data.Add(list1);
            data.Add(list2);
            return data;

        }

        public ActionResult Calculate()
        {
            return View();
        }
        
    }
}

